<template>
  <div class="app-container">
    <el-form
      ref="dataForm"
      :rules="rules"
      :model="postForm"
      label-position="right"
      label-width="100px"
      style="width: 400px; margin-left:50px;"
    >
      <el-form-item label="类型" prop="name">
        <el-input v-model="postForm.name" clearable placeholder="类型" maxlength="20" />
      </el-form-item>
      <el-form-item label="说明" prop="description">
        <el-input v-model="postForm.remarks" clearable placeholder="说明" maxlength="200" />
      </el-form-item>
      <el-form-item>
        <el-button @click="back()">返回</el-button>
        <el-button type="primary" @click="submitForm">确定</el-button>
      </el-form-item>
    </el-form>
  </div>
</template>

<script>
import { addSysApitype, updateSysApitype, getSysApitype } from '@/api/sys/apitype'
import msgUtil from '@/utils/msgUtil'
import routerUtil from '@/utils/routerUtil'

const defaultForm = {
  id: null,
  name: null,
  remarks: null
}
export default {
  name: 'SysApitypeDetail',
  props: {
    isEdit: {
      type: Boolean,
      default: false
    }
  },
  data() {
    return {
      postForm: Object.assign({}, defaultForm),
      rules: {
        name: [
          { required: true, trigger: 'blur', message: '接口类型不能为空' }
        ]
      },
      defaultProps: {
        children: 'children',
        label: 'label'
      }
    }
  },
  created() {
    if (this.isEdit) {
      this.getSysApitype(this.$route.query.id)
    } else {
      this.loading = false
    }
  },
  methods: {
    // 修改接口类型
    updateSysApitype() {
      updateSysApitype(this.postForm).then(response => {
        msgUtil.success()
        routerUtil.backPath(this, '/sys/apitype/index')
      })
    },
    // 新增接口类型
    addSysApitype() {
      addSysApitype(this.postForm).then(response => {
        msgUtil.success()
        routerUtil.backPath(this, '/sys/apitype/index')
      })
    },
    // 返回
    back() {
      routerUtil.backPath(this, '/sys/apitype/index')
    },
    // 提交form
    submitForm() {
      this.$refs.dataForm.validate(valid => {
        if (valid) {
          if (this.isEdit) {
            this.updateSysApitype()
          } else {
            this.addSysApitype()
          }
        }
      })
    },
    // 获取角色信息并赋值给当前页面
    async getSysApitype(id) {
      var json = { 'id': id }
      await getSysApitype(json).then(response => {
        this.postForm = response.data
      })
    }
  }
}
</script>
<style scoped>

</style>
